/*
 * @Author: your name
 * @Date: 2021-01-14 11:02:00
 * @LastEditTime: 2021-03-16 14:00:27
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wx-x-mai\pages\surpassingtable\checkthescore\checkthescore.js
 */
/** 
 * @see 该页面是查询成绩的页面
 */
import { getXnxqList } from '../../../utils/util'
import { API_CONFIG } from '../../../config'
const xnxqList = getXnxqList()
Page({
  data: {
    index: xnxqList.length - 1,
    xnxqList,
    crrent: 0,
    url: null,
    looksize: "查看说明",
    userInfo: '', // 用户信息
    bgStyle: `background-image: url("${API_CONFIG.staticUrl}wx-x-mai/bg.jpg")`, // 背景图片
    sTotalCredits: '', // 在校总学分
    sYearAverage: '', // 历年平均分
    sTermCredits: '', // 学期总学分
    sTermAverage: '', // 学期平均分
    aGradeListData: [], // 查询成绩数据
    truecontent: false,
    contentname: "本功能已通过您成绩授权，您的各科成绩已换算超越值，您单科的成绩将以超越百分数展示。本次排位仅统计已同意科目成绩授权的用户，可能因部分同学未授权排位，导致学力排位产生偏差、波动。次数据仅为您对各学科整体掌握程度提供参考，无实际排名意义",
    appMsg: {
      type: '', // 状态
      show: false // 是否展示
    },
    container: '信息提交后不可修改请确认信息无误',

  },
  onLoad () {
    this.setData({ userInfo: wx.getStorageSync('userInfo') })
    this.gradeInfo()

  },
  clicktabs () {
    wx.navigateTo({
      url: "/pages/web/web?url=" + 'https://www.baidu.com/'
    })
  },
  goBaidu () {
    wx.navigateTo({
      url: "/pages/weburl/weburl"
    })
  },
  /**
   * @see 获取查询成绩列表
   */
  gradeInfo () {
    const { xnxqList, index } = this.data
    const request = {
      schoolYear: xnxqList[index].id
    }
    this.setData({
      appMsg: {
        type: 'loading',
        show: true
      }
    })
    wx.$API['schoolStudy/getStuGrade'](request).then((response) => {
      this.setData({
        appMsg: {
          type: 'loading',
          show: false
        }
      })


      const data = response.data

      data.forEach(item => {
        if (item.rate == 0) {
          return 0;
        }
        item.rate = Number(item.rate * 100).toFixed();   //将小数点转换为整形浮点，然后再乘以100后保留两位小数
        item.rate += "%"
        item.truedata = false
        item.moresize = "查看超越值"
      });
      const dataLength = response.data.length
      if (dataLength === 0) {
        this.setData({
          appMsg: {
            type: 'none',
            show: true
          }
        })
      } else {
        this.setData({
          sTotalCredits: data[0].totalCredits,
          sYearAverage: data[0].yearAverage,
          sTermCredits: data[0].termCredits,
          sTermAverage: data[0].termAverage,
          aGradeListData: data
        })
      }
    }).catch((error) => {
      // 防止直接进入查成绩页面，token无效请求失败
      if (error === 'Invalid token') {
        this.gradeInfo()
      } else {
        this.setData({
          appMsg: {
            type: 'err',
            show: true
          }
        })
      }
    })
  },

  //查看更多
  lookmore (e) {
    // var moresize = e.currentTarget.dataset.item.moresize;
    const index = e.target.dataset.index;
    let temp = 'aGradeListData[' + index + '].truedata';
    this.setData({
      [temp]: true
    })
  },

  lookingdetail () {
    this.setData({
      truecontent: !this.data.truecontent
    })
    if (this.data.truecontent == true) {
      this.setData({
        looksize: "隐藏"
      })
    }
    else {
      this.setData({
        looksize: "查看说明"
      })
    }
  },
  /**
   * @see 点击刷新页面
   */
  clickMsgButton () {
    this.gradeInfo()
  },
  /**
   * @see 点击选择下拉学年学期
   */
  bindPickerChange (e) {
    this.setData({
      index: e.detail.value
    })
    this.gradeInfo()
  }
})
